﻿<%@ Page Language="C#" %>
<%@ Import Namespace="IRL.Utilities" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.SqlClient" %>
<script runat="server">
    public const int CURRENT_MODULE = 52;

    int organization, pid = 0;
    bool HasWriteAccess = false;

    protected void Page_Load(object src, EventArgs e)
    {
        Security SysAcc = new Security(CURRENT_MODULE);
        // check is authenticate
        SysAcc.Authenticate();
        HasWriteAccess = SysAcc.IsAuthorized(2);

        // get company id
        if (Request.QueryString["oid"] != null)
            try { organization = Convert.ToInt32(Request.QueryString["oid"]); }
            catch { organization = 0; }
        
        // get proposal id
        if (Request.QueryString["pid"] != null)
            try { pid = int.Parse(Request.QueryString["pid"]); }
            catch { pid = 0; };

        //Save to database when getting postback data
        if (Page.IsPostBack)
        {
            if (HasWriteAccess)
            {                
                saveData();                
            }

            if (pid > 0 && organization > 0)
            {
                Response.Redirect("listContactPersons.aspx?pid=" + pid + "&oid=" + organization);
            }
            else
            {
                Response.Redirect("default2.aspx");
            }

        }
        else
        {
            loadData();
        }


        // add control attribute
        txtEmail.Attributes.Add("onkeypress", "return KeyCheck(event)");
    }

    void saveData()
    {
        int companyID = Convert.ToInt32(txtCompanyID.Text);
        string title = cboTitle.SelectedValue;
        string firstName = txtFname.Text;
        string lastName = txtLname.Text;
        string jobTitle = txtJTitle.Text;
        string phone = txtPhone_1.Text+" "+txtPhone_2.Text+" "+txtPhone_3.Text;
        string email = txtEmail.Text;


        object connStr = ConfigurationSettings.AppSettings["sqlConn"];
        using (SqlConnection conn = new SqlConnection(connStr.ToString()))
        {
            SqlCommand cmd = new SqlCommand("sp_SaveContact", conn);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add("@companyid", SqlDbType.Int).Value = companyID;
            cmd.Parameters.Add("@title", SqlDbType.VarChar, 10).Value = title;
            cmd.Parameters.Add("@firstname", SqlDbType.NVarChar, 30).Value = firstName;
            cmd.Parameters.Add("@lastname", SqlDbType.NVarChar, 40).Value = lastName;
            cmd.Parameters.Add("@jobtitle", SqlDbType.NVarChar, 120).Value = jobTitle;
            cmd.Parameters.Add("@phone", SqlDbType.NVarChar, 14).Value = phone.Trim();
            cmd.Parameters.Add("@email", SqlDbType.NVarChar, 100).Value = email;            
            conn.Open();
            cmd.ExecuteNonQuery();
        }
    }

    void loadData()
    {
        List.fillList(cboTitle, "sp_Select_cms_Lookup 'title', @ValueOnly=1");
        if (!HasWriteAccess)
        {
            SaveButton.Visible = false;
        }

        object connStr = ConfigurationSettings.AppSettings["sqlConn"];
        using (SqlConnection conn = new SqlConnection(connStr.ToString()))
        {
            conn.Open();
            //Load Client
            if (organization > 0)
            {
                //cboOrganization.SelectedValue = Request.QueryString["oid"].ToString();
                SqlCommand cmd = new SqlCommand("SELECT [Company_Name] FROM [Tbl_Company_Info] WHERE [Company_ID]=" + organization + ";", conn);
                ltlCompanyName.Text = "<b>" + (String)cmd.ExecuteScalar() + "</b>";
                txtCompanyID.Text = organization.ToString();
            }            

        }
    }
</script>

<html xmlns="http://www.w3.org/1999/xhtml">

<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />

    <title>Indochina Research</title>
    <link type="text/css" rel="stylesheet" href="../css/global.css" />
    <link type="text/css" rel="stylesheet" href="css/cms.css" />
        
    <script language="javascript" type="text/javascript" src="includes/functions.js"></script>
    
    <script language="javascript">
        function Save() {
	        var f = document.frmContact;
	        missinginfo = "";
        	
	        // Check First name
	        if(f.txtFname.value == ""){
		        missinginfo += "\t- First name \n";
	        }
	        // Check Last Name
	        if(f.txtLname.value == ""){
		        missinginfo += "\t- Last Name \n";
	        }
        			
	        // Check Job Title
	        if(f.txtJTitle.value==""){
		        missinginfo += "\t- Job Tilte\n";
	        }
        	
	        // Check Mobile
	        if((f.txtPhone_1.value.length+f.txtPhone_2.value.length+f.txtPhone_3.value.length)>0){
		        if(!f.txtPhone_1.value.match(/^\d+$/) || f.txtPhone_1.value.length<3){
			        f.txtPhone_1.select();
			        missinginfo += "\t- Mobile Phone\n";
		        }else if(!f.txtPhone_2.value.match(/^\d+$/) || f.txtPhone_2.value.length<2){
			        f.txtPhone_2.select();
			        missinginfo += "\t- Mobile Phone\n";
		        }else if(!f.txtPhone_3.value.match(/^\d+$/) || f.txtPhone_3.value.length<6){
			        f.txtPhone_3.select();
			        missinginfo += "\t- Mobile Phone\n";
		        }
	        }
	        
	        // Check Email
	        if(f.txtEmail.value.length>0)
	        {
	            if(!validEmail(f.txtEmail.value))
	            {
	                missinginfo += "\t- Email\n";
	            }
	        }
        	
	        if (missinginfo != "") {
		        msg = "__________________________\n";
		        msg += "You failed to correctly fill in your:\n\n";
		        msg += missinginfo;
		        msg += "_________________________\n";
		        msg += "Please complete mandatory fields before saving";
		        alert(msg);
		        return ;
	        }else
		        msg = "This Information has been saved into the database";
        		
        	
	        //alert(msg);
	        f.submit();
	        //return true;
        }
        // check keypress for email
        function KeyCheck(e)
        {
            // For keydown 
            // var KeyID = (window.event) ? e.keyCode : e.which + e.keyCode;
            var KeyID = (window.event) ? e.keyCode : e.which;
            return str(KeyID);
        }
    </script>

</head>

<body>
<table width="773" border="0" cellspacing="0" cellpadding="0" align="center">
	<tr>
		<td width="765" height="540" align="center" valign="top" style="border:solid 1px #10147e;" >
			<!-- header -->
			<table border="0" cellpadding="0" cellspacing="0" width="100%">
				<tr bgcolor="#F4F4F9">
					<td colspan="3" align="right" style="padding:10px;">
						<b class="blueL">Client Management System</b>
					</td>
				</tr>
				<tr>
					<td class="wave_l">&nbsp;</td>
					<td><img src="images/wave_logo.jpg" width="107" height="83"></td>
					<td class="wave_r">&nbsp;</td>
				</tr>
				<tr>
					<td colspan="3" style="padding:0 20px;">
						<div style="background-color:#bcb3d5; padding:5px 10px;">
							<span style="float:right">
							    <a class="blue" href="javascript:history.go(-1);">&laquo; Back</a>
							</span>
							<span class="pathmap">
								<a href="default.aspx" >CPanel</a> :: 
								<a href="default2.aspx" >Ops Files</a> :: 
								Save new Contact
						    </span>
						</div>
						<br />
						
						
					</td>
				</tr>
				
			</table>
			
			<!-- body -->
			<form id="frmContact" method="post" runat="server">
			    <asp:TextBox ID="txtCompanyID" runat="server" Visible="false" />
			    <table border="0" cellspacing="0" cellpadding="3">
			        <tr>
			            <td colspan="3" align="center">
			                <asp:Literal ID="ltlCompanyName" runat="server" /></td>
			        </tr>
			        <tr>
			            <td>&nbsp;</td>
			        </tr>
					<tr>
					  <td align="right">Title </td>
					  <td>:</td>
					  <td><asp:DropDownList ID="cboTitle" runat="server" /></td>
				  </tr>
				    <tr>
					  <td align="right">First Name </td>
					  <td>:</td>
					  <td><asp:TextBox ID="txtFname" size="25" MaxLength="30" runat="server" />
                        <span class="red">*</span> &nbsp;
						Last Name:
                         <asp:TextBox ID="txtLname" size="25" MaxLength="40" runat="server" />
                         <span class="red">*</span></td>
				    </tr>
					<tr>
					  <td align="right">Job Title   </td>
					  <td>:</td>
					  <td><asp:TextBox ID="txtJTitle" size="32" MaxLength="120" runat="server" /> <span class="red">*</span> </td>
				  </tr>
					<tr>
					  <td align="right">Mobile</td>
					  <td>:</td>
					  <td>+ <asp:TextBox ID="txtPhone_1" CssClass="date" size="2" MaxLength="3" runat="server" />
                            <asp:TextBox ID="txtPhone_2" CssClass="date" size="1" MaxLength="2" runat="server" />
                            <asp:TextBox ID="txtPhone_3" CssClass="date" size="8" MaxLength="8" runat="server" />
                            <span class="fldNote">Example: +855 23 215184</span></td>
				  </tr>
					<tr>
					  <td align="right">Email</td>
					  <td>:</td>
					  <td>
					  	<asp:TextBox ID="txtEmail" size="30" MaxLength="50" runat="server" /></td>
				  </tr>
					
				  <tr>
				  	<td align="center" colspan="3">&nbsp;</td>
				  </tr>
				  
				  <tr>
				  	<td colspan="2">&nbsp;</td>
					<td>
						<table id="SaveButton" border="0" cellpadding="2" cellspacing="0" runat="server" >
							<tr>
								<td width="70">
									<a class="cmd" href="javascript:Save();">Save</a></td>
								<td width="5">&nbsp;</td>
							
								<td width="80">
								    <a class="cmd" href="javascript:history.go(-1);">Cancel</a></td>
							</tr>
						</table></td>
				  </tr>
				</table>
			</form>
			
		</td>
		
		<td width="6" class="rightShadow" valign="top">
			<img src="images/white.gif" width="6" height="6" /></td>
	</tr>
	
	<!-- bottom shadow -->
	<tr>
		<td height="6" class="bottomShadow">
			<img src="images/white.gif" width="6" height="6" /></td>
		
		<td width="6" height="6">
			<img src="images/shadow_corner.gif" width="6" height="6" /></td>
	</tr>
</table>


</body>
</html>
